#define DEBUG
#include <cstdio>

using namespace std;

typedef long long ll;

const int maxlen=16;
const ll maxn=1000000000000000000;

ll f[maxlen+1];

void initF() {
  f[0] = 0;
  for (int i=1; i<=maxlen; i++) {
    f[i] = f[i-1]+i*15*(1LL<<(i*4-4));
    if (f[i]>maxn) {
      break;
    }
  }
}

ll posToNum(ll n, int &rest, int &len) {
  for (len=1; f[len]<=n && len<maxlen; len++);
  rest = (n-f[len-1])%len;
  return (1LL<<(len*4-4))-1+(n-f[len-1])/len;
}

//Tip:From low to high
char getNumChar(ll n, int rest) {
  int x=(n>>(rest*4-4))&15;
  return x<10 ? '0'+x : 'A'+x-10;
}

ll countChar(ll n, char c) {
  int x = '0'<=c && '9'>=c ? c-'0' : c-'A'+10, i;
  ll ret=0;
  for (i=1; (1LL<<(i*4-4))<=n; i++) {
    ret += ((n>>(i*4)))*(1LL<<(i*4-4))-(!x)*(1LL<<(i*4-4));
    if (((n>>(i*4-4))&15)>x) {
      ret += (1LL<<(i*4-4));
    } else if (((n>>(i*4-4))&15)==x) {
      ret += n-((n>>(i*4-4))<<(i*4-4))+1;
    }
  }
  return ret;
}

int main() {
  freopen("hex.in", "r", stdin);
  freopen("hex.out", "w", stdout);

  initF();

  int t;
  scanf("%d", &t);
  for (int i=1; i<=t; i++) {
    int typ, rest, len;
    ll n, num;
    scanf("%d %lld", &typ, &n);
    if (typ==1) {
      num = posToNum(n, rest, len);
      printf("%c\n", rest ? getNumChar(num+1, len-rest+1) : getNumChar(num, 1));
    } else {
      char c;
      scanf(" %c ", &c);
      num = posToNum(n, rest, len);
      int ans=0;
      for (int i=1; i<=rest; i++) {
	ans += getNumChar(num+1, len-i+1)==c;
      }
      printf("%lld\n", countChar(num, c)+ans);
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
